<?php
class InstallController extends Controller {

    protected function _init(){
        $configPath = C('APP_FULL_PATH').'/../config.php';
        if(file_exists($configPath)) {
            Header("Location: /admin/index.php");
        }
        header("Content-Type:text/html; charset=utf-8");
    }

    public function IndexAction(){
        $this->display();
    }

    public function InstallAction(){
        $configPath = C('APP_FULL_PATH').'/../config.php';
        $DB = trim($_POST["DB"]);
        $DB_USER = trim($_POST["DB_USER"]);
        $DB_PWD = trim($_POST["DB_PWD"]);
        $DB_HOST = trim($_POST["DB_HOST"]);
        $DB_PORT = trim($_POST["DB_PORT"]);
        $SITE = trim($_POST["SITE"]);
        $CONFIG = array(
                'DB_HOST' => $DB_HOST,
                'DB_PORT' => $DB_PORT,
                'DB_NAME' => $DB,
                'DB_USER' => $DB_USER,
                'DB_PWD'  => $DB_PWD,
                'PATH_MOD'=> 'NORMAL',
            );
        $sqlArr = array();
        $sqlArr['history'] = <<<EOT
CREATE TABLE IF NOT EXISTS `history` (
  `id` int(11) NOT NULL,
  `type` tinyint(1) NOT NULL COMMENT '1普通，2元年，3头条',
  `startDate` datetime NOT NULL,
  `endDate` datetime DEFAULT NULL,
  `headline` varchar(50) NOT NULL,
  `text` text NOT NULL,
  `tag` varchar(255) DEFAULT NULL,
  `asset` text NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
EOT;
        $sqlArr['timeline'] = <<<EOT
CREATE TABLE IF NOT EXISTS `timeline` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` tinyint(1) NOT NULL,
  `startDate` datetime NOT NULL,
  `endDate` datetime DEFAULT NULL,
  `headline` varchar(50) NOT NULL,
  `text` text NOT NULL,
  `tag` varchar(255) DEFAULT NULL,
  `asset` text NOT NULL,
  `status` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
EOT;
        $sqlArr['user'] = <<<EOT
CREATE TABLE IF NOT EXISTS `user` (
  `username` varchar(20) NOT NULL,
  `password` varchar(32) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
EOT;
        if(isset($DB) && isset($DB_USER) && isset($DB_PWD) && isset($DB_HOST) && isset($DB_PORT)){
            $db = DB::getInstance($CONFIG);
            foreach ($sqlArr as $table => $sql) {
                $ret = $db->execute($sql);
                if($ret === false){
                    halt("创建表$table失败");
                    break;
                }
            }
            $str = '<?php return ' . var_export($CONFIG, true) . ';';
            $ret = file_put_contents($configPath, $str);
            if($ret === false){
                halt('配置文件写入失败，请确保网站根目录有写权限');
            }
        }else{
            halt('请输入完整的数据库信息');
        }
        if(isset($_POST['USER']) && isset($_POST['PWDONCE']) && isset($_POST['PWDTWICE']) && trim($_POST['PWDONCE']) === trim($_POST['PWDTWICE'])){
            $db = M();
            $USER = $db->escape(trim($_POST["USER"]));
            $PWDONCE = $db->escape(trim($_POST["PWDONCE"]));
            $PWDTWICE = $db->escape(trim($_POST["PWDTWICE"]));
            $pwd = passwordHash($PWDONCE);
            $db->execute("INSERT INTO  `user` (`username`,`password`) VALUES ('$USER','$pwd')");
            $db->execute("INSERT INTO  `timeline` (`type` ,`startDate` ,`endDate` ,`headline` ,`text` ,`tag` ,`asset` ,`status`) VALUES ('1', now() , NULL ,  'test',  'test', NULL , '{media:\'\',caption:\'\',credit:\'\'}', '1')");
            echo "<script>alert('安装成功！'); window.location.href='/admin/index.php';</script>";
        }else{
            halt('请输入正确的用户信息');
        }
    }
}
